package com.alibaba.dubbo.filter;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author zhangjuntao
 * @create 2017-10-26 下午4:23
 **/
public class FilterTest {

    private static final List<FilterElementChain> LIST = Lists.newArrayList();

    public static void main(String[] args) {

        Invocation last = msg -> System.out.println("最终dubbo要执行的任务");

        LIST.add((invocation, msg) -> {
            System.out.printf("Filter Chain 1-msg:%s\n", msg);
            invocation.invoke(msg);
        });
        LIST.add((invocation, msg) -> {
            System.out.printf("Filter Chain 2-msg:%s\n", msg);
            invocation.invoke(msg);
        });
        LIST.add((invocation, msg) -> {
            System.out.printf("Filter Chain 3-msg:%s\n", msg);
            invocation.invoke(msg);
        });


        for (final FilterElementChain filterElementChain : LIST) {
            final Invocation next = last;
            last = msg -> {
                System.out.printf("Invocation 1-msg:%s\n", msg);

                /**
                 * next : 用于保存invocation的下一个执行单元invocation。虽然next单元没有保存。
                 */
                filterElementChain.invoke(next,msg);
            };
        }

        last.invoke("000000");
    }
}
